package com.example.chcrm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.chcrm.entity.common.BasicDoc;
import com.example.chcrm.entity.common.LineConfig;
import com.example.chcrm.entity.common.ResponseMsg;
import com.example.chcrm.entity.common.ServletUtils;
import com.example.chcrm.entity.employee.Department;
import com.example.chcrm.entity.employee.EmployeeVo;
import com.example.chcrm.entity.employee.RoleModels;
import com.example.chcrm.service.LineConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author 邹
 * @Date 2025/4/23 13:21
 * @Version 1.0
 */
@RestController
@RequestMapping("/sys/lineConfig")
@CrossOrigin // 允许跨域访问
public class LineConfigController {
    @Autowired
    private LineConfigService lineConfigService;
    /**
     * 部门新增
     */
    @PostMapping("/add")
    public ResponseMsg<Object> add(@RequestBody LineConfig lcf) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if (lcf.getLineType() != 1 && lcf.getLineType() != 2) {
            msg.fail("操作失败，线路类型格式不正确");
            return msg;
        }
        if (lcf.getLineType() == 2) {
            if (StringUtils.isBlank(lcf.getDeparturePortId())) {
                msg.fail("操作失败，起始港口id不能为空");
                return msg;
            }
            if (StringUtils.isBlank(lcf.getArrivePortId())) {
                msg.fail("操作失败，到达港口id不能为空");
                return msg;
            }
        }
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
//        if(empVo.getAccount().equals("admin")){
//            msg.fail("admin账号不能操作");
//            return msg;
//        }
        return  lineConfigService.add(lcf,empVo);
    }

    /**
     * 禁用/启用
     */
    @PostMapping("/disable")
    public ResponseMsg<Object> disable(@RequestBody LineConfig roleModels) {
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return  lineConfigService.disable(roleModels,empVo);
    }

    /**
     * 查询
     *
     * essEmployee
     * @return
     */
    @PostMapping("/pageList")
    public ResponseMsg<IPage<LineConfig>> pageList(@RequestBody LineConfig roleModels) {
        return lineConfigService.pageList(roleModels);
    }

    /**
     * 角色修改
     */
    @PostMapping("/update")
    public ResponseMsg<Object> update(@RequestBody LineConfig lcf) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if (!StringUtils.isNotBlank(lcf.getId())) {
            msg.fail("操作失败，操作id不能为空");
            return msg;
        }
        if (lcf.getLineType() != BasicDoc.LINE_TYPE_1 && lcf.getLineType() != BasicDoc.LINE_TYPE_2) {
            msg.fail("操作失败，线路类型格式不正确");
            return msg;
        }
        if (lcf.getLineType() == BasicDoc.LINE_TYPE_2) {
            if (StringUtils.isBlank(lcf.getDeparturePortId())) {
                msg.fail("操作失败，起始港口id不能为空");
                return msg;
            }
            if (StringUtils.isBlank(lcf.getArrivePortId())) {
                msg.fail("操作失败，到达港口id不能为空");
                return msg;
            }
        }
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return lineConfigService.update(lcf, empVo);
    }

    /**
     * 角色删除
     */
    @GetMapping("/delete/{id}")
    public ResponseMsg<Object> delete(@PathVariable String id) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if (id == null || id.equals("")) {
            msg.fail("删除失败ID不能为空");
            return msg;
        }
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return lineConfigService.delete(id,empVo);
    }

    @GetMapping("/detail/{id}")
    public ResponseMsg<Object> detail(@PathVariable String id) {
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return lineConfigService.detail(id);
    }
}
